<?php

/**
 * 代支付类
 * 属性:id,memberId,amount,code,remark,type,status,url,processFee
 */
class Application_Model_AgentPay extends Application_Model_DomainObject implements Application_Model_PayInterface{
    
    protected $_id;
    
    protected $_memberId;
    
    protected $_payMoney;
    
    protected $_code;
    
    protected $_expressCompany;

    protected $_remark;
    
    protected $_type;
    
    protected $_status;
    
    protected $_url;
    
    protected $_processFee;
    
    protected $_dateCreated;
    
    protected $_dateProcee;

     /**
     * 未付款
     */
    const STATUS_nopay = 0;
    /**
     * 已付款
     */
    const STATUS_acpay = 1;
    /**
     * 已处理
     */
    const STATUS_prpay = 2;
    /**
     * 已退款
     */
    const STATUS_Refund = -1;
    /**
     * 货到付款
     */
    const TYPE_Ingstore = 1;
    /**
     * 淘宝代付款
     */
    const TYPE_Taobao = 2;
    /**
     * 最低标准VIP等级
     */
    const Agent_UserGrade = 1;
    
    public function getTypeName(){
        $names =array(1=>'货到付款',2=>'淘宝代付款');
        return $names[$this->_type];
    }
    
    public function getStatusText(){
        return Application_Model_PayBill::getStatusText($this->_status);
    }
    
    /**
     * 返回状态对应描述
     * @return string
     */
    public function getstatuscaption()
    {
        switch ($this->_status)
        {
            case "0";
               return '未付款';
            case "1";
               return '已付款';
            case "2";
               return '已处理';
            case "-1";
               return '已退款';
           default :
               return '未付款';
        }
    }


    public function canPay($userId) {
        if($this->_status == Application_Model_PayBill::PAY_STATUS_SUCCESS)
            return false;
        if($this->_memberId != $userId)
            return FALSE;
        return TRUE;
    }

    public function getMoney() {
        return $this->_payMoney + $this->_processFee;
    }
    
    public function getPayName() {
        return '悠奇-代支付';
    }
    
    public function getPayCommen() {
        return "代支付流水号：".$this->_id;
    }
    
    public function isRepeatPay() {
        if($this->_status == self::STATUS_nopay){
            return true;
        } 
        return false;
    }
    
    public function setPaid() {
        $this->_status = Application_Model_PayBill::PAY_STATUS_SUCCESS;
        
    }
    
    public function getPayDescription() {
        return '悠奇代支付服务，支付号：'.$this->Id.'用户名：'.(Application_Model_User::load($this->_memberId)->email);
    }
    
    public function getBillNo() {
        return $this->_id;
    }
    
    public static function getByFilter($userId,$start,$pageSize){
        return Application_Model_AgentPay::getDao()->queryPage(array('memberId'=>$userId), 'id desc', $pageSize, $start);
        
    }
    
    public static function getFilter($startTime,$endTime,$start,$pagesize){
          $where = array();
          $where[ " status > "]= 0;
          $where[" and dateProcee >= "] = $startTime;
          if($endTime){
                $where[" and dateProcee <= "] = $endTime." 23:59:59";
          }
//          var_dump($where);
        return Application_Model_AgentPay::getDao()->queryPage($where, 'id desc', $pagesize, $start);
        
    }
    
    public static function getFilterAll($startTime,$endTime){
          $where = array();
          $where[ " status > "]= 0;
          $where[" and dateProcee >= "] = $startTime;
          if($endTime){
                $where[" and dateProcee <= "] = $endTime." 23:59:59";
          }
//          var_dump($where);
        return Application_Model_AgentPay::getDao()->queryAll($where, 'id desc');
        
    }
    /**
    * 获取代支付列表
    * @author wuzw
    * @param string $username 昵称
    * @param int $count 页行数
    * @param int $offset 页
    * @return array object 列表对象
    */
   public static function getByFilter2($agentPaySearch,$count, $offset)
   {
        return self::getDao()->queryPage($agentPaySearch->getSearchFileds(), " dateCreated desc ", $count, $offset);
   }
    /**
     * 获取当天代支付款数量
     */
    public static function getcountagentpay($status=null)
    {
         if($status === null){
            $where[' dateCreated >='] = date('Y-m-d');
        }else{
            $where[' status = '] = $status;
        }
        return Application_Model_AgentPay::getDao()->count($where);
    }
    
    /**
    * 根据userid，加载user对象
    * @author wuzw
    * @return type
    */
   public function getuserinfo()
   {
       if($this->_memberId)
       {
           return Application_Model_User::load($this->_memberId);
       }
   }
   
   public function getuserstoreno()
   {
       if($this->_memberId)
       {
           $Item = Application_Model_User::load($this->_memberId);
           return $Item->storeno;
       }
   }
   
   /**
    * 处理代支付
    * @param string $idlist
    * @return boolean
    */
   public function proceeAgentPay($idlist,$money)
    {
        $arrid = explode(",",$idlist);
        foreach ($arrid as $key => $value) {
            if($value == ''){
                continue;
            }
            $agent = Application_Model_AgentPay::load($value);
            $time = date('Y-m-d H:i:s',time());
            if($agent)
            {
                $agent->dateProcee = $time;
                $agent->status = Application_Model_AgentPay::STATUS_prpay;
                $agent->save();
                Application_Model_Operate::addOperate(($agent->payMoney+$agent->processFee), null, $agent->id, "代支付处理", $agent->memberId);

            }
        }
        return true;
    }
}